<!DOCTYPE html>
<html>
	<head>
		<meta charset="utf-8">
		<title>提示词分类管理</title>
		<meta name="renderer" content="webkit">
		<meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">
		<meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1">
		<meta name="apple-mobile-web-app-status-bar-style" content="black">
		<meta name="apple-mobile-web-app-capable" content="yes">
		<meta name="format-detection" content="telephone=no">
		<!-- jQuery-->
		<script type="text/javascript" src="/static/plugins/jquery/jquery-3.6.0.min.js"></script>

		<!-- layui-->
		<link rel="stylesheet" href="/static/plugins/layui/2.7.6/css/layui.css" media="all">
		<script src="/static/plugins/layui/2.7.6/layui.js" charset="utf-8"></script>

		<!-- common -->
		<link rel="stylesheet" href="/static/admin/css/common.css" media="all" />
		<link rel="stylesheet" href="/static/admin/css/mini-ease.css" media="all" />
		
		
	</head>
	<style>
		.all-view{
			width: 90%;
			margin: 0 auto;
			margin-top: 20px;
		}
		.btn-view{
			margin-bottom: 10px;
			
		}
		.add-btn{
			width:150px;
		}
		.bottom-view{
			position: fixed;
			bottom: 0;
			width: 100%;
		}
		.add-btn-view{
			width: 150px;
			margin: 0 auto;
			margin-bottom: 40px;
		}
		.btnColor{
			background-color: gray;
		}
	</style>
	
	<body>

	<!-- 列表 -->
	<div class="layui-row layui-col-space5" style="width:90%;margin: 0 auto;">

		<div class="layui-col-md12" id="id_panel_left_grid">
			<div class="layui-panel" style="overflow: auto; padding-top: 0.2rem;">
				<div class="mid-content">

					<table class="layui-hide" id="main_table" lay-filter="main_table"></table>

					<script type="text/html" id="toolbarDemo">
						<div class="layui-row" style="height: 40px;">
							<div class="layui-col-xs12">
								<button type="button" class="layui-btn layui-bg-blue layui-btn-sm btn-view"  lay-on="add-data" id="addBtn">
									<i class="layui-icon layui-icon-add-1 layui-font-12"></i> 新增
								</button>
							</div>

						</div>
					</script>
					<script type="text/html" id="templet_index_no">{{d.LAY_TABLE_INDEX+1}}</script>
				</div>
			</div>
		</div>
	</div>

	<div class="bottom-view">
		<div class="layui-btn-container add-btn-view">
			<button type="button" class="layui-btn layui-btn-primary layui-border-blue add-btn" onclick="toClose();">关闭并刷新</button>
		</div>
	</div>

		<input type="hidden" name="id" id="id" value=""><!--场景id-->


      <script>
		  $('#id').val('[(${dto.id})]');

		  var dataList = [];

		  $.ajax({
			  url : '/api/inter/eKnowTemplateType/searchType',
			  data : {id:'[(${dto.id})]'},
			  type : 'POST',
			  async : false,
			  success : function(result) {
				  if (result.code == '0') {
					  var resDate = result.data;
					  for(var i=0;i<resDate.length;i++){
						  dataList.push({
							  id:resDate[i].id,
							  sort: resDate[i].sort,
							  typeName: resDate[i].typeName,
						  });
					  }
				  }else{
					  layer.msg(result.msg);
				  }
			  }
		  });

		  layui
				  .extend({optimizeSelectOption: '/static/plugins/layui/extends/optimizeSelectOption/optimizeSelectOption'})
				  .use(['form', 'element', 'table', 'dropdown', 'optimizeSelectOption', 'util', 'laydate'], function(){
					  var element = layui.element;
					  var table = layui.table;
					  var dropdown = layui.dropdown;
					  var form = layui.form;
					  var util = layui.util;
					  var $ = layui.jquery;
					  var laydate = layui.laydate;


					  var cols =[ //表头
						  [
							  {title: '排序',   width: '200' , field:'sort' ,align:"center", edit: 'text'},
							  {title: '分类', width: '480' , field:'typeName', align:"center", edit: 'text'},
							  {title: '操作', width: '200' , align:"center",templet:
										  function(d){
							  					var str = '';
							  					if(d.id==0)str += '<button type="button" class="layui-btn layui-btn-primary layui-border-blue layui-btn-sm" onclick="toAdd();" id="addBtn'+d.id+'"><i class="layui-icon layui-icon-ok layui-font-12"></i> 保存</button>';
											  	str +='<button type="button" class="layui-btn layui-btn-primary layui-border-red layui-btn-sm" onclick="toDel(\''+d.id+'\');"><i class="layui-icon layui-icon-delete layui-font-12"></i> 删除</button>';
											  	return str;
										  }
							  },
						  ]
					  ];
					  // 创建渲染实例
					  table.render({
						  id:'main_table'
						  ,elem: '#main_table'
						  //,url:'/api/inter/knowledgeType/searchType' // 此处为静态模拟数据，实际使用时需换成真实接口
						  ,data:dataList
						  ,toolbar: '#toolbarDemo'
						  ,defaultToolbar: [ 'filter',{
							  title: '刷新页面'
							  ,layEvent: 'LAYTABLE_REFRESH'
							  ,icon: 'layui-icon-refresh'
						  }]
						  ,height: window.innerHeight - 150 // 最大高度减去其他容器已占有的高度差
						  ,where: {id:'[(${dto.id})]'}
						  ,cellMinWidth: 95
						  ,page: false
						  ,request: {
							  pageName: 'page',
							  limitName: 'size'
						  }
						  //,skin: 'row'
						  ,even: true // 开启隔行背景
						  ,skin: 'grid'//设置表格边框风格
						  ,cols:cols
						  ,done: function(res){
							  console.log(res);
						  }
						  ,error: function(res, msg){
							  console.log(res, msg)
						  }
					  });

					  // 工具栏事件
					  table.on('toolbar(main_table)', function(obj){
						  var id = obj.config.id;
						  var checkStatus = table.checkStatus(id);
						  var othis = lay(this);
						  switch(obj.event){
							  case 'LAYTABLE_REFRESH':
							  {
								  layer.confirm("确定刷新吗？",{
									  btn: ['确定', '取消']
								  }, function (index) {
									  location.reload();
									  layer.close(index);
								  });
								  break;
							  }
						  };
					  });


					  // 单元格编辑事件
					  table.on('edit(main_table)', function(obj){
						  var field = obj.field; // 得到修改的字段
						  var value = obj.value; // 得到修改后的值
						  var data = obj.data; // 得到所在行所有键值
						  console.log(obj); // 查看对象所有成员

						  // 值的校验
						  /*
						  if(value.replace(/\s/g, '') === ''){
							  layer.tips('值不能为空', this, {tips: 1});
							  return;
						  }
						  if(data.sort.replace(/\s/g, '') === ''){
							  layer.tips('排序不应未空', this, {tips: 1});
							  return;
						  }
						  if(!isNumber(data.sort)){
							  layer.tips('排序应为数字', this, {tips: 1});
							  return;
						  }
						  
						   */
						  if(data.typeName.length>10){
							  layer.tips('分类名称长度最好不要大于10', this, {tips: 1});
							  return;
						  }

						  if(data.id!=0){
							  toUpdate(data.id,data.sort,data.typeName);
						  }else {
							  if(data.typeName!=null && data.typeName!='' &&  data.sort!=null && data.sort!='' ){
								  toAdd();
							  }
						  }



						  // 更新当前缓存数据
						  var update = {};
						  update[field] = value;
						  obj.update(update, true); // 参数 true 为 v2.7 新增功能，即同步更新其他包含自定义模板并可能存在关联的列视图
					  });

					  // 普通事件
					  util.on('lay-on', {
						  //新增
						  "add-data": function(){
							  dataList.push({
								  id:0,
								  sort:"",
								  typeName: "",
							  });
							  $('#addBtn').hide();
							  table.reloadData('main_table', { data: dataList});
						  }
					  })


				  });

		  //删除
		  function toDel(id) {
			  if(id==0){
			  	var dataListNew = [];
			  	for(var i=0;i<dataList.length;i++){
			  		if(dataList[i].id!=0){
						dataListNew.push(dataList[i]);
					}
				}
				  dataList = dataListNew;
				  $('#addBtn').show();
				  layui.table.reloadData('main_table', { data: dataList});
			  }else {
				  layer.confirm('确定要删除该分类?', {icon: 3}, function () {
					  //点击确定的回调
					  layui.layer.msg('请稍后', {
						  icon: 16,
						  shade: 0.01
					  });
					  delYes(id);
				  }, function () {});//点击取消的回调
			  }
		  }


		  //确定删除
		  function delYes(id){
			  $.ajax({
				  url: '/api/inter/eKnowTemplateType/delEKnowTemplateType',
				  data: {
					  'id':id
				  },
				  type: 'POST',
				  async: true,
				  success: function (result) {
					  if(result.code=='SUCCESS'){
						  layer.close();
						  layer.msg("操作成功", {icon: 1});
						  var dataListNew = [];
						  for(var i=0;i<dataList.length;i++){
							  if(dataList[i].id!=id){
								  dataListNew.push(dataList[i]);
							  }
						  }
						  dataList = dataListNew;
						  layui.table.reloadData('main_table', { data: dataList});
					  }else if(result.code=='CANNOT'){
						  layer.close();
						  layer.alert('此分类不为空，无法删除。若要删除，请先清空该分类！', {
							  icon: 0,
							  shadeClose: true,
							  title: '提示 '
						  })
					  }else if(result.code=='HASTIMERSETNODEL'){
						  layer.close();
						  layer.alert('此分类已与定时任务绑定，无法删除。若要删除，请先前往定时表单分类绑定解绑！', {
							  icon: 0,
							  shadeClose: true,
							  title: '提示 '
						  })
					  }else {//失败
						  layer.close();
						  layer.msg("删除失败，请稍后再试！", {icon: 2});
					  }
				  },
				  error: function () {
					  layer.close();
					  layer.msg("删除失败，请稍后再试！", {icon: 2});
				  }
			  });


		  }

		  //修改
		  function toUpdate(id,sort,typeName) {
			  if(sort ==undefined || sort ==null || sort =='' ){
				  layer.msg('请进行排序', {icon: 0});
				  return ;
			  }
			  if(!isNumber(sort)){
				  ayer.msg('排序应为数字', {icon: 0});
				  return ;
			  }
			  if(typeName ==undefined || typeName ==null || typeName =='' ){
				  layer.msg('请填写分类名称', {icon: 0});
				  return ;
			  }
			  if(typeName.length>10){
				  layer.msg('分类名称长度最好不要大于10', {icon: 0});
				  return ;
			  }

			  layui.layer.msg('保存中', {icon: 16,shade: 0.01});
			  $.ajax({
				  url: '/api/inter/eKnowTemplateType/addOrUpdateEKnowTemplateType',
				  data: {
					  'addOrUpdate':'update',
					  'id':id,
					  'sceneId':$('#id').val(),
					  'sort':sort,
					  'typeName':typeName,
				  },
				  type: 'POST',
				  async: true,
				  success: function (result) {
					  layer.close();
					  console.log('result:',result);
					  if(result.code=='SUCCESS'){
						  layer.msg("修改成功", {icon: 1});
					  } else{//失败
						  layer.close();
						  layer.msg('不好意思，系统出现问题了，请稍后重试！', {icon: 2});
					  }
				  },
				  error: function () {
					  layer.close();
					  layer.msg('不好意思，系统出现问题了，请稍后重试！', {icon: 2});
				  }
			  });


		  }
		  //新增
		  function toAdd() {
			  var sort = null;
			  var typeName = null;
			  for(var i=0;i<dataList.length;i++){
				  if(dataList[i].id==0){
					  sort = dataList[i].sort;
					  typeName = dataList[i].typeName;
				  }
			  }

			  if(sort ==undefined || sort ==null || sort =='' ){
				  layer.msg('请进行排序', {icon: 0});
				  return ;
			  }
			  if(!isNumber(sort)){
				  ayer.msg('排序应为数字', {icon: 0});
				  return ;
			  }
			  if(typeName ==undefined || typeName ==null || typeName =='' ){
				  layer.msg('请填写分类名称', {icon: 0});
				  return ;
			  }
			  if(typeName.length>10){
				  layer.msg('分类名称长度最好不要大于10', {icon: 0});
				  return ;
			  }

			  layui.layer.msg('保存中', {icon: 16,shade: 0.01});
			  $.ajax({
				  url: '/api/inter/eKnowTemplateType/addOrUpdateEKnowTemplateType',
				  data: {
					  'addOrUpdate':'add',
					  'sceneId':$('#id').val(),
					  'sort':sort,
					  'typeName':typeName,
				  },
				  type: 'POST',
				  async: true,
				  success: function (result) {
					  layer.close();
					  console.log('result:',result);
					  if(result.code=='SUCCESS'){
						  layer.msg("操作成功", {icon: 1});
						  $('#addBtn').show();
						  location.reload();
						  //$('#addBtn'+0).hide();
					  } else{//失败
						  layer.close();
						  layer.msg('不好意思，系统出现问题了，请稍后重试！', {icon: 2});
					  }
				  },
				  error: function () {
					  layer.close();
					  layer.msg('不好意思，系统出现问题了，请稍后重试！', {icon: 2});
				  }
			  });
		  }

		  function isNumber(value) {
			  return /^-?\d+(\.\d+)?$/.test(value);
		  }


		  function toClose() {
			  window.parent.location.reload();

		  }
      </script>

  </body>
</html>